<?php
	require_once "includes/class/member_management.class.php";
	require_once('AmazonAPI/.config.inc.php');
	require_once('AmazonAPI/CBUI/CBUIRecipientTokenPipeline.php');
	
	if($_SESSION['user_id_sess']=="") {
		echo "<script type='text/javascript'> window.location='index.php?signin&url=profile'; </script>";
	}
	if(isset($_REQUEST['signature']) && !empty($_REQUEST['signature'])) {
			$signature = $_REQUEST['signature'];
			$refundTokenID = $_REQUEST['refundTokenID'];
			$signatureVersion = $_REQUEST['signatureVersion'];
			$signatureMethod = $_REQUEST['signatureMethod'];
			$certificateUrl = $_REQUEST['certificateUrl'];
			$tokenID = $_REQUEST['tokenID'];
			$callerReference = $_REQUEST['callerReference'];
			$sqlUpdUsr = "UPDATE tbl_users SET signature1='".$signature."',
						refundTokenID1='".$refundTokenID."',
						signatureVersion1='".$signatureVersion."',
						signatureMethod1='".$signatureMethod."',					
						certificateUrl1='".$certificateUrl."',
						tokenID1='".$tokenID."',
						callerReference1='".$callerReference."' WHERE user_id='$userId'";
			$rstUpdUsr = mysql_query($sqlUpdUsr) or die(mysql_error());
			if($rstUpdUsr) { 
				$msg = "Your tokenID is successfully set.";
			}else{
				$msg = "Your tokenID is not set please try again!";
			}
		}	
	if($_REQUEST['token_id']!=1)
	{
		$userId = $_SESSION['user_id_sess'];	
		$usrObj = new member_management;
		$rand = rand(100000, 10000000);
		
		$sqlSelItem_comm="SELECT * FROM tbl_users where user_id=".$userId." ";
		$rstSelItem_comm=mysql_query($sqlSelItem_comm);
		$rowSelItem_comm=mysql_fetch_array($rstSelItem_comm);
		if(is_array($rowSelItem_comm))
		{
			$usr_type=$rowSelItem_comm['user_type'];
			if((int)$usr_type==1)
			{
				$pay_type = "ABT,ACH";		
			}
			else if((int)$usr_type==2)
			{
				$pay_type = "CC,ABT,ACH";
			}
			else if((int)$usr_type==3)
			{
				$pay_type = "CC,ABT,ACH";	
			}
			else if((int)$usr_type==4)
			{
				if($rowSelItem_comm['refundTokenID']=='')
				{
					$pay_type = "ABT,ACH";
				}
				else
				{
					$pay_type = "CC,ABT,ACH";
				}
				
			}
			else
			{
				$pay_type="CC,ABT,ACH";
			}	
						
		}	
		//echo $pay_type.'   '.$usr_type;
		$pipeline = new Amazon_FPS_CBUIRecipientTokenPipeline(AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY);
			
			$pipeline->setMandatoryParameters("swapen".rand(),  
					H_ROOT."index.php?token_id=1", "0", "2.9", "True");
					
			$pipeline->addParameter("paymentMethod", $pay_type);
			$re_URL = $pipeline->getUrl();
			header("location:".$re_URL);
	}
	else
	{
		if($_REQUEST['token_id']=='1')
		{	
			$sqlPost="update tbl_refer_comm set isPaid='2'
				  where refer_by_user_id='".$_SESSION['user_id_sess']."' and isPaid='0' AND DATEDIFF( CURDATE( ) , buy_date ) >=30";
			$rstPost=mysql_query($sqlPost) or die(mysql_error());	
			$strMessage .="Your withdrawal request has been sent successfully<br/>";		
			$withdraw="";	
			header("location:index.php?refer&ref=1");		
		}
		
		//echo 'hi';
	}
	//print_r($userInfo);	
?>
